<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div style="padding:20px 0px 0px 20px;">灰色表示该人员已在其它班组中</div>
                <div id="member" style="padding:20px;"></div>
            </div>
        </div>
    </div>
</div>

<script src="/layui/layui.js"></script>
<script src="/js/common.js"></script>
<script>
    layui.config({
        base: '/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'transfer'], function () {
        var $ = layui.$;
        var transfer = layui.transfer;
        var teamId = getParam('teamId');

        transfer.render({
            id: 'member',
            elem: '#member',
            title: ['组外人员', '组内人员'],
            showSearch: true,
            height: 400,
            onchange: function (data, side) {
                if (side == 0) {
                    for (let i in data) {
                        addMember(data[i].value);
                    }
                } else if (side == 1) {
                    for (let i in data) {
                        removeMember(data[i].value);
                    }
                }
            }
        })

        $.get('team-members', {teamId}, function (result) {
            var data = result.data;
            var value = [];
            for (let i in data) {
                if (data[i]) {
                    value.push(data.id);
                }
            }
            transfer.reload('member', {data, value});
        });

        function addMember(userId) {
            $.ajax({
                url: 'team-members',
                type: 'POST',
                data: {teamId, userId}
            });
        }

        function removeMember(userId) {
            $.ajax({
                url: 'team-members',
                type: 'DELETE',
                data: {teamId, userId}
            });
        }
    });
</script>
</body>
</html>